//
//  MyViewController.m
//  AddressBook
//
//  Created by mac on 2017/8/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "MyViewController.h"
#import "PesonalInformationViewController.h"
#import "LoginViewController.h"

@interface MyViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *hearView;
@property (nonatomic, strong) NSDictionary * userData;
@property (nonatomic, strong) UIButton *pullOut;
@property (nonatomic, strong) UserDataModel *userDataModel;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    [self.tableView addSubview:self.pullOut];
    self.userDataModel = [UserDataModel getLocalModel];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else{
        return 3;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0;
}
- (CGFloat )tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 80.0;
    }else{
        return 44.0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellStr];
    }
    if (indexPath.section == 0) {
        [cell.contentView addSubview:self.hearView];
//        cell.textLabel.text = @"姓名";
//        cell.detailTextLabel.text = @"账号";
    }else if (indexPath.section == 1){
        cell.textLabel.text = @"设置";
    }else{
        if (indexPath.row == 0) {
            cell.textLabel.text = @"关于我们";
        }else if (indexPath.row == 1) {
            cell.textLabel.text = @"联系我们";
        }else if (indexPath.row == 2) {
            cell.textLabel.text = @"意见反馈";
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        self.hidesBottomBarWhenPushed = YES;
        PesonalInformationViewController *pesonalVC = [[PesonalInformationViewController alloc]init];
        [self.navigationController pushViewController:pesonalVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }
    
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsHorizontalScrollIndicator = NO;
//        _tableView.tableHeaderView = self.hearView;
    }
    return _tableView;
}

-
(UIView *)hearView{
    if (!_hearView) {
        _hearView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 80)];
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(15*ScaleW, 5, ScreenW - (15*ScaleW)*2, 40)];
        
        //nameLabel.text = [NSString stringWithFormat:@"姓名：%@",self.userDataModel.userinfo[@"member_name"]];
        nameLabel.textColor = [UIColor grayColor];
        nameLabel.font = [UIFont systemFontOfSize:20];
        [_hearView addSubview:nameLabel];
        UILabel *accountLabel = [[UILabel alloc]initWithFrame:CGRectMake(15*ScaleW, CGRectGetMaxY(nameLabel.frame)+10 , ScreenW - (15*ScaleW)*2, 20)];
        //accountLabel.text = [NSString stringWithFormat:@"账号：%@",self.userDataModel.userinfo[@"phone_name"][0][@"phone"]];
        accountLabel.textColor = [UIColor grayColor];
        accountLabel.font = [UIFont systemFontOfSize:15];
        [_hearView addSubview:accountLabel];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenW - 35*ScaleW, 25, 20, 30)];
        imageView.image = [UIImage imageNamed:@"rl_logo"];
        [_hearView addSubview:imageView];
    }
    return _hearView;
}

-(UIButton *)pullOut{
    if (!_pullOut) {
        _pullOut = [[UIButton alloc]initWithFrame:CGRectMake(20, ScreenH - 200, ScreenW - 40, 40)];
        [_pullOut setTitle:@"退出当前账号" forState:UIControlStateNormal];
        _pullOut.backgroundColor = [UIColor colorWithRed:230/255.0 green:79/255.0 blue:71/255.0 alpha:1];
        _pullOut.layer.cornerRadius = 2;
        
        [_pullOut addTarget:self action:@selector(pullOutBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pullOut;
}

-(void)pullOutBtn{
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:LOGIN_SUCCESSFUL];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:KUSERINFORMATION];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:KUSERPASSWORD];
    [self presentViewController:loginVC animated:NO completion:nil];
    
}

@end
